package com.example.controller.customer;

import com.example.constant.JwtClaimsConstant;
import com.example.dto.CustomerDTO;
import com.example.dto.CustomerLoginDTO;
import com.example.entity.Customer;
import com.example.properties.JwtProperties;
import com.example.result.Result;
import com.example.service.CustomerService;
import com.example.utils.JwtUtil;
import com.example.vo.CustomerLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * (Customer)表控制层
 *
 * @author makejava
 * @since 2024-03-21 23:03:02
 */
@RestController
@RequestMapping("/customer/customer")
@Slf4j
@Api(tags = "微信用户相关接口")
public class CustomerController {
    /**
     * 服务对象
     */
    @Resource
    private CustomerService customerService;

    @Autowired
    private JwtProperties jwtProperties;


    @PostMapping("/login")
    @ApiOperation("微信登录")
    public Result<CustomerLoginVO> login(@RequestBody CustomerLoginDTO customerLoginDTO){
        log.info("微信用户登录：{}",customerLoginDTO.getCode());
        Customer customer = customerService.wxLogin(customerLoginDTO);
        //为微信用户生成jwt令牌
        Map<String,Object> claims = new HashMap<>();

        claims.put(JwtClaimsConstant.USER_ID,customer.getId());
        String token = JwtUtil.createJWT(jwtProperties.getUserSecretKey(),jwtProperties.getUserTtl(),claims);

        CustomerLoginVO userLoginVO = CustomerLoginVO.builder()
                .id(customer.getId())
                .openid(customer.getOpenid())
                .token(token)
                .avatar(customer.getAvatar())
                .isVip(customer.getIsVip())
                .name(customer.getName())
                .phone(customer.getPhone())
                .build();
        return Result.success(userLoginVO);
    }

    @PutMapping("")
    @ApiOperation("修改个人信息")
    public Result updateUserInfo(@RequestBody CustomerDTO customerDTO){
        log.info("修改个人信息：{}",customerDTO.getId());
        Result result = new Result<>();
        result = customerService.updateInfo(customerDTO);
        return result;
    }

}

